#include "mybutton.h"
#include <QDebug>

//MyButton::MyButton(QWidget *parent) : QPushButton(parent){

//}
MyButton::MyButton(QString normalImg, QString pressImg){
    normalImgPath = normalImg;
    pressImgPath = pressImg;


    bool res = pix.load(normalImgPath);


    if(!res){
        qDebug() << "加载图片失败！";
        return;
    }


//    qDebug() << "按钮已创建";

    //设置图片固定大小
    this->setFixedSize(pix.width(), pix.height());

    //设置不规则图片样式表
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    //设置图标大小
    this->setIconSize(QSize(pix.width(), pix.height()));
    //geometry 几何 动画
    animation = new QPropertyAnimation(this, "geometry");
}

void MyButton::zoom1(){
    //设置实践间隔
    animation->setDuration(200);
    //设置起始位置
    animation->setStartValue(QRect(this->x(),this->y()+10,this->width(),this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线 弹跳效果
//    animation->setEasingCurve(QEasingCurve::OutBounce);
    //开始动画
    animation->start();

}

void MyButton::zoom2(){
    //设置实践间隔
    animation->setDuration(200);
    //设置起始位置
    animation->setStartValue(QRect(this->x(),this->y()-10,this->width(),this->height()));
    //设置结束位置
    animation->setEndValue(QRect(this->x(),this->y(),this->width(),this->height()));
    //设置缓和曲线 弹跳效果
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    animation->start();
}

//重新鼠标按下事件，切换按钮图标
void MyButton::mousePressEvent(QMouseEvent * e){
    if(this->pressImgPath != ""){
        QPixmap pix;
        bool res = pix.load(this->pressImgPath);
        if(!res){
            qDebug() << "图标加载失败";
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mousePressEvent(e);
}
//重新鼠标松开事件，切换按钮默认图标
void MyButton::mouseReleaseEvent(QMouseEvent *e){
    if(this->pressImgPath != ""){
        QPixmap pix;
        bool res = pix.load(this->normalImgPath);
        if(!res){
            qDebug() << "图标加载失败";
            return;
        }
        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    return QPushButton::mouseReleaseEvent(e);
}

